<template>
  <el-dialog
    title="新增员工"
    :visible="dialogVisible"
    width="50%"
    @close="closeDialog"
    @open="openDialog"
  >
    <el-form ref="employeeRef" :model="employeeForm" :rules="employeerules" label-width="100px" class="demo-ruleForm">
      <el-form-item label="姓名" prop="username">
        <el-input v-model="employeeForm.username" placeholder="请输入姓名" style="width: 90%" />
      </el-form-item>
      <el-form-item label="手机号" prop="mobile">
        <el-input v-model="employeeForm.mobile" placeholder="请输入手机号" style="width: 90%" />
      </el-form-item>
      <el-form-item label="入职时间" prop="timeOfEntry">
        <el-date-picker
          v-model="employeeForm.timeOfEntry"
          style="width: 90%"
          placeholder="请选择入职时间"
        />
      </el-form-item>
      <el-form-item label="聘用形式" prop="formOfEmployment">
        <el-select v-model="employeeForm.formOfEmployment" placeholder="请选择聘用形式" style="width: 90%">
          <el-option label="正式" value="1" />
          <el-option label="非正式" value="2" />
        </el-select>
      </el-form-item>
      <el-form-item label="工号" prop="workNumber">
        <el-input v-model="employeeForm.workNumber" placeholder="请输入工号" style="width: 90%" />
      </el-form-item>
      <el-form-item label="部门" prop="departmentName">
        <el-cascader
          v-model="employeeForm.departmentName"
          placeholder="请选择"
          style="width: 90%"
          :options="list"
          :props="{ checkStrictly: true, label: 'name', value: 'name' }"
          :show-all-levels="false"
        />
      </el-form-item>
      <el-form-item label="转正时间" prop="correctionTime">
        <el-date-picker
          v-model="employeeForm.correctionTime"
          style="width: 90%"
          placeholder="请选择转正时间"
        />
      </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="closeDialog">取 消</el-button>
      <el-button type="primary" @click="submit">确 定</el-button>
    </span>
  </el-dialog>
</template>

<script>
import { getDepartmentList } from '@/api/department'
import { recurTransTree } from '@/utils/transTree'
import { addEmployee } from '@/api/employee' // 导入方法位置错误 Object(...) is not a function
export default {
  props: {
    dialogVisible: {
      required: true,
      type: Boolean
    }
  },
  data() {
    return {
      employeeForm: {
        username: '',
        mobile: '',
        formOfEmployment: '',
        workNumber: '',
        departmentName: '',
        timeOfEntry: '',
        correctionTime: ''
      },
      employeerules: {
        username: [
          { required: true, message: '用户姓名不能为空', trigger: ['blur', 'change'] },
          { min: 1, max: 4, message: '用户姓名为1-4位', trigger: ['blur', 'change'] }
        ],
        mobile: [
          { required: true, message: '手机号不能为空', trigger: ['blur', 'change'] },
          { pattern: /^1[3-9]\d{9}$/, message: '手机号格式不正确', trigger: ['blur', 'change'] }
        ],
        formOfEmployment: [
          { required: true, message: '聘用形式不能为空', trigger: ['blur', 'change'] }
        ],
        workNumber: [
          { required: true, message: '工号不能为空', trigger: ['blur', 'change'] }
        ],
        departmentName: [
          { required: true, message: '部门不能为空', trigger: ['blur', 'change'] }
        ],
        timeOfEntry: [
          { required: true, message: '请选择入职时间', trigger: ['blur', 'change'] }
        ]
      },
      list: []
    }
  },
  methods: {
    closeDialog() {
      this.$emit('closeDialog')
      this.$refs.employeeRef.resetFields()
      this.employeeForm.departmentName = ''
    },
    async openDialog() {
      const res = await getDepartmentList()
      console.log(res)
      this.list = recurTransTree(res.data.depts, '')
    },
    submit() {
      this.$refs.employeeRef.validate(async valid => {
        if (!valid) return

        await addEmployee({
          ...this.employeeForm,
          departmentName: this.employeeForm.departmentName[this.employeeForm.departmentName.length - 1]
        })
        this.$message.success('新增员工成功')
        this.$emit('closeDialog')
        this.$emit('get-employee-list')
      })
    }
  }
}
</script>

<style>

</style>
